﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using AuchanScan.Datas;
using AuchanScan.Models;

namespace AuchanScan.ViewModels
{
    public class TrophyViewModel
    {
        public TrophyViewModel()
        {
            CurrentTrophies = new ObservableCollection<TrophyBase>();
            CurrentSimulatedTrophies = new ObservableCollection<TrophyBase>();

            CurrentTrophies = ViewModelLocator.MainViewModelStatic.Datas.Trophies;
        }

        public ObservableCollection<TrophyBase> CurrentTrophies { get; private set; }
        public ObservableCollection<TrophyBase> CurrentSimulatedTrophies { get; private set; }

        public string SimulationResultTitle { get; set; }
        public string SimationResultDescription { get; set; }

        public void Simulate(List<Achat> achats)
        {
            CurrentSimulatedTrophies.Clear();
            List<TrophyBase> wonTrophies = UserData.GetInstance().Trophies.Where(t => t.HasBeenWon).ToList();

            List<TrophyResult> results = TrophyHelper.Current.CheckTrophies(achats);

            SimulationResultTitle = "Simulation Congrates !";
            SimationResultDescription = "Vous aurez plein de trophées. Hourra!";

            if (results.Count > 0)
            {
                bool shouldCheckTrophies = wonTrophies != null && wonTrophies.Count > 0;

                foreach (TrophyResult r in results)
                {
                    if (shouldCheckTrophies)
                    {
                        if (wonTrophies.FirstOrDefault(t => t.Id == r.Id) == null)
                            CurrentSimulatedTrophies.Add(TrophyHelper.Current.AllTrophies.First(t => t.Id == r.Id));
                    }
                    else
                    {
                        CurrentSimulatedTrophies.Add(TrophyHelper.Current.AllTrophies.First(t => t.Id == r.Id));
                    }
                }
            }
        }

        public void ResetSimulation()
        {
            SimulationResultTitle = string.Empty;
            SimationResultDescription = string.Empty;
        }
    }
}